package com.lewis.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.lewis.entry.User;
import com.lewis.mapper.RoleVoMapper;
import com.lewis.mapper.UserVoMapper;
import com.lewis.service.UserServiceImpl;
import com.lewis.vo.RoleVo;
import com.lewis.vo.UserVo;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@RestController
@EnableSwagger2
public class UserController {
	
	@Autowired
	@Qualifier("userVoMapper")
	private UserVoMapper userVoMapper;
	
	@Resource
	private RoleVoMapper roleVoMapper;
	
	
	/**
	 * 查詢某一個個用戶的信息(一個用戶，多個角色)
	 * @param username
	 * @return
	 */
	@GetMapping("/getUserVo/{username}")
	public UserVo userVoTest(@PathVariable("username")String username){
		return userVoMapper.getUserVo(username);
	}
	
	/**
	 * 查詢擁有某個角色的所有用戶(多個用戶，一個角色)
	 * @param role
	 * @return
	 */
	@GetMapping("/getRoleVo/{role}")
	public RoleVo roleVoTest(@PathVariable("role") String role){
		
		System.out.println(role);
		return roleVoMapper.getRoleVo(role);
		
	}
	
	@Resource
	private UserServiceImpl userServiceImpl;
    
	/**
	 * test transition
	 * @throws Exception 
	 */
	@PostMapping("/addUser")
	public void testAddUser(@RequestBody UserVo user) throws Exception{
		
		userServiceImpl.addUserAndRole(user);
		
	}
	
	/**
	 * 添加数据到缓存的测试
	 * @param user
	 * @throws Exception 
	 */
	@PostMapping("/testAddUserToCache")
	public void testAddUserToCacheDemo(@RequestBody User user) throws Exception{
		userServiceImpl.testAddUserToCache(user);		
	}
	
	/**
	 * 通过username从缓存中查询
	 * @param username
	 * @return
	 * @throws Exception
	 */
	
	@GetMapping("/getUserFromCache/{username}")
	public List<User>  testGetUserFromCache(@PathVariable("username")String username) throws Exception{
		return userServiceImpl.getUserFromCache(username);
	}
	
	/**
	 * 删除用户信息
	 * @param username
	 * @throws Exception 
	 */
	@PostMapping("/deleteFromCache")
	public String  testDeleteCache(@RequestBody String username) throws Exception{
		
		userServiceImpl.testDeleteCache(username);
		return "success";
	}
}
